/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.google.cse.emea.universaltransformer.transformers;

import com.google.cse.emea.universaltransformer.editors.SimpleTextEditor;
import com.google.cse.emea.jct.utils.Profile;
import com.google.cse.emea.universaltransformer.*;

import com.google.cse.emea.universaltransformer.editors.EditorFactory;
import com.google.cse.emea.universaltransformer.editors.EditorInterface;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import javax.swing.JFrame;
import javax.swing.JPanel;

import static java.util.logging.Level.*;
import static com.google.cse.emea.universaltransformer.TaskList.*;
import static com.google.cse.emea.universaltransformer.TransformLogger.*;

/**
 *
 * @author jtrapp
 */
public class XSLTModule extends AbstractXSLTModule {

    public static final String XSLT_LABEL = "xslt";
    private XMLInputPanel inputPanel = null;
    private XMLOutputPanel outputPanel = null;

    public XSLTModule() {

    }

    @Override
    public String getName() {
        return XSLT_LABEL;
    }

    @Override
    public JPanel getInputPanel() {
        if (inputPanel == null) {
            ActionListener validateActionListener = new ActionListener() {

                public void actionPerformed(java.awt.event.ActionEvent evt) {
                    String filename = profile.get(XSLTProfileEnum.INPUT_FILE);
                    validate(filename);
                }
            };
            ActionListener showSchemaActionListner = new ActionListener() {

                public void actionPerformed(ActionEvent e) {
                    String filename = profile.get(XSLTProfileEnum.INPUT_SCHEMA);
                    SimpleTextEditor.getEditor().viewFile(filename, null, "test/xml", "UTF-8");

                }
            };
            inputPanel = new XMLInputPanel(validateActionListener, showSchemaActionListner);
        }
        return inputPanel;
    }

    @Override
    public JPanel getOutputPanel() {
        if (outputPanel == null) {
            outputPanel = new XMLOutputPanel();
        }
        return outputPanel;
    }

    @Override
    public void setProfile(Profile profile) {
        super.setProfile(profile);
        getInputPanel(); //Force generating
        String inputFilename = profile.get(XSLTProfileEnum.INPUT_FILE);
        if ((inputFilename != null) && inputFilename.length() > 0) {
            File file = new File(inputFilename);
            if (file.exists() && file.isFile()) {
                inputPanel.updateUI(true, profile.get(AbstractXSLTProfileEnum.INPUT_SCHEMA));

            } else {
                inputPanel.updateUI(false, profile.get(AbstractXSLTProfileEnum.INPUT_SCHEMA));
            }

        }

    }

    /** Parse the xml file from the archive and generate a dom document for further use. 
     * 
     * @return
     */
    @Override
    public boolean parse(String inputFile) {
        if (profile == null) {
            return false;
        }
        if (checkChanged(inputFile)) {
            return super.parse(inputFile);
        } else {
            return hasDocument();
        }
    }

    public Object[] getProfileKeys() {
        return XSLTProfileEnum.values();
    }
}
